package com.qc.blogsystemplus.controller;

import com.qc.blogsystemplus.bean.ResponseBean;
import com.qc.blogsystemplus.entity.User;
import com.qc.blogsystemplus.mapper.UserMapper;
import com.qc.blogsystemplus.service.UserService;
import com.qc.blogsystemplus.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestController
public class LoginController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public ResponseBean Login(@RequestBody User user) {
        User userDb = userService.getUser(user.getUsername());
        if (userDb != null &&  userDb.getPassword().equals(user.getPassword())) {
            return new ResponseBean(200,"登陆成功", JWTUtil.sign(user.getUsername(),user.getPassword()));
        } else {
            return new ResponseBean(401,"用户名或密码错误",null);
        }
    }

    @RequestMapping(path = "/401")
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public ResponseBean unauthorized() {
        return new ResponseBean(401, "未授权或授权不合法", null);
    }
}
